<template>
  <div>
    <div class="bg-white flex flex-col justify-center pt-5 my-5">
      <BasicForm @register="registerForm" />
    </div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="AuthCode.permissionAdd">
          新增菜单
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
              auth: AuthCode.permissionUpdate,
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              auth: AuthCode.permissionDestroy,
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
  import { nextTick } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { useDrawer } from '/@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';
  import { useForm, BasicForm } from '/@/components/Form';

  import { columns, searchFormSchema } from './data';
  import { getMenuDetail, getMenuTree } from '/@/api/sys/menu';
  import { Recordable } from 'vite-plugin-mock';
  import { AuthCode } from '/@/settings/authSetting';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();

  const [registerForm, { getFieldsValue }] = useForm({
    labelWidth: 120,
    schemas: searchFormSchema,
    submitFunc: getMenuList,
  });
  async function getMenuList() {
    setLoading(true);
    let data = await getMenuTree(getFieldsValue());
    await setTableData(data);
    setLoading(false);
    return new Promise<void>(() => {});
  }
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, expandAll, setTableData, setLoading }] = useTable({
    title: '菜单列表',
    api: getMenuTree,
    rowKey: 'id',
    columns,
    isTreeTable: true,
    pagination: false,
    striped: false,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: false,
    actionColumn: {
      width: 80,
      dataIndex: 'action',
      title: '操作',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
  });
  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }

  async function handleEdit(record: Recordable) {
    let data = await getMenuDetail(record.id);
    openDrawer(true, {
      record: data,
      isUpdate: true,
    });
  }

  function handleDelete(record: Recordable) {
    console.log(record);
    createMessage.warning('暂不支持此功能');
  }

  function handleSuccess() {
    reload();
  }

  function onFetchSuccess() {
    // 演示默认展开所有表项
    nextTick(expandAll);
  }
</script>

<style scoped lang="less"></style>
